package com.ai.service.networking.impl;

import com.ai.entity.comm.SysNetty;
import com.ai.mapper.comm.SysNettyMapper;
import com.ai.service.networking.ISysNettyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-04-03 21:17:12
 */
@Service
public class SysNettyServiceImpl extends ServiceImpl<SysNettyMapper, SysNetty> implements ISysNettyService {

    /**
     * 根据IP和端口号获取netty
     *
     * @param ip
     * @param port
     * @return
     */
    @Override
    public SysNetty getByIP(String ip, int port) {
        List<SysNetty> sysNettyList = list(new QueryWrapper<SysNetty>().eq("ip", ip));
        if (sysNettyList.size() == 1) {
            return sysNettyList.get(0);
        }
        return getOne(new QueryWrapper<SysNetty>().eq("ip", ip).eq("port", port));
    }

    /**
     * 根据code获取netty
     *
     * @param code
     * @return
     */
    @Override
    public SysNetty getByteCode(String code) {
        return getOne(new QueryWrapper<SysNetty>().eq("code", code));
    }

    /**
     * 获取本机的所有服务端
     */
    @Override
    public List<SysNetty> getNativeServerList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 1));
    }

    /**
     * 获取本机的所有客户端
     */
    @Override
    public List<SysNetty> getNativeClientLIst() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 2));
    }


    /**
     * 获取本机的所有自启动的服务端
     */
    @Override
    public List<SysNetty> getNativeStartServerList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 1).eq("is_start", 1));
    }

    /**
     * 获取本机的素有自启动的客户端
     */
    @Override
    public List<SysNetty> getNativeStartClientList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 2).eq("is_start", 1));
    }


    /**
     * 获取非本机的所有服务端
     */
    @Override
    public List<SysNetty> getNonNativeServerList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 2).eq("netty_type", 1));
    }

    /**
     * 获取非本机的所有客户端
     */
    @Override
    public List<SysNetty> getNonNativeClientLIst() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 2).eq("netty_type", 2));
    }
}
